package com.ruihang.generalibrary.ui.adapter.holder;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.ruihang.generalibrary.ui.util.IHolderClick;
import com.ruihang.generalibrary.ui.util.OnItemInternalCheckChangedListener;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;

import org.xutils.x;

/**
 * Created by Ly on 2016/11/30.
 */
public abstract class BaseRecylerHolder<T> extends RecyclerView.ViewHolder {

    protected Context context;
    /**
     * 是否已经销毁
     */
    protected boolean isDestoried;

    protected OnItemInternalClick internalClick;

    protected OnItemInternalCheckChangedListener internalCheckChangedListener;

    protected IHolderClick holderClick;

    protected int type;

    protected boolean isAct = false;

    public BaseRecylerHolder(Context context, View itemView) {
        super(itemView);
        this.context = context;
        initInject();
    }

    protected void initInject() {
        x.view().inject(this, itemView);
    }

    public abstract void setData(T t);

    public void setData(T t, boolean isEditing) {
        setData(t);
    }

    public void setData(T t, int position, boolean isSelected) {
        setData(t);
        if (isSelected) {
            onHolderSelect();
        } else {
            onHolderUnSelect(-1, position);
        }
    }

    public void setData(T t, int position, boolean isSelected, boolean isEditing) {
        setData(t, position, isSelected);
    }

    /**
     * 当holder消除前掉用的方法
     * 主要用于结束线程等异步的东西
     * <p>
     * 此方法需在相应的adapter中调用
     */
    public void onDetached() {
    }

    public void onDestory() {
        isDestoried = true;
        itemView.setOnClickListener(null);
    }

    /**
     *
     */
    public void onHolderUnSelect(int selectPos, int oldSelectPos) {
    }

    public void onHolderSelect() {
    }

    public BaseRecylerHolder setInternalClick(OnItemInternalClick internalClick) {
        this.internalClick = internalClick;
        return this;
    }

    public BaseRecylerHolder setInternalCheckChangedListener(OnItemInternalCheckChangedListener internalCheckChangedListener) {
        this.internalCheckChangedListener = internalCheckChangedListener;
        return this;
    }

    public BaseRecylerHolder setHolderClick(IHolderClick holderClick) {
        this.holderClick = holderClick;
        return this;
    }

    public boolean isEditing() {
        return false;
    }

    public void setSelected(boolean isSelected, boolean callListener) {
    }

    public void setSelected(boolean isSelected) {
        setSelected(isSelected, true);
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setAct(boolean isAct) {
        this.isAct = isAct;
    }

    protected void doInternalClick(View v) {
        if (internalClick != null) {
            internalClick.onItemInternalClick(itemView, v, getAdapterPosition());
        }
    }
}
